﻿using Gateway.Common.Models;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Volo.Abp;
using Volo.Abp.Modularity;

namespace Gateway.SqlSugar
{
    [DependsOn()]
    public class SqlSugarAbpModule : AbpModule
    {
        public override Task ConfigureServicesAsync(ServiceConfigurationContext context)
        {
            var configuration = context.Services.GetConfiguration();
            var queryConnection = configuration["SlaveConnectionString"];
            if (string.IsNullOrEmpty(queryConnection))
                throw new ArgumentNullException($"{nameof(queryConnection)} not null");
            context.Services.AddSqlSugar(configuration["ConnectionSetting"]!,
                JsonConvert.DeserializeObject<List<QueryConnectionModel>>(queryConnection)!);

            return base.ConfigureServicesAsync(context);
        }

        public override Task OnApplicationInitializationAsync(ApplicationInitializationContext context)
        {
            return base.OnApplicationInitializationAsync(context);
        }
    }
}